package com.shenmazong.serveredustudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.serveredustudent.mapper.*;
import com.shenmazong.serveredustudent.pojo.*;
import com.shenmazong.serveredustudent.service.TbClazzTeacherService;
import com.shenmazong.serveredustudent.vo.ClazzListVo;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbClazzTeacherServiceImpl extends ServiceImpl<TbClazzTeacherMapper, TbClazzTeacher>
    implements TbClazzTeacherService{

    @Autowired
    private TbClazzTeacherMapper clazzTeacherMapper;

    @Autowired
    private TbTeacherMapper tbTeacherMapper;

    @Autowired
    private TbClazzMapper tbClazzMapper;

    @Autowired
    private TbMajorMapper majorMapper;

    @Autowired
    private TbStageMapper stageMapper;

    @Override
    public ResultResponse saveTeacherToClazz(TbClazzTeacher tbTeacher) {
        if(tbTeacher==null ){
            return ResultResponse.FAILED(-1,"参数异常");
        }

        LambdaQueryWrapper<TbClazz> last = new QueryWrapper<TbClazz>().lambda().eq(TbClazz::getClazzId, tbTeacher.getClazzId()).last("limit 1 ");

        TbClazz clazz = tbClazzMapper.selectOne(last);
        if(clazz==null){
            return ResultResponse.FAILED(-1,"参数异常");
        }

        LambdaQueryWrapper<TbTeacher> last1 = new QueryWrapper<TbTeacher>().lambda().eq(TbTeacher::getTeacherId, tbTeacher.getTeacherId()).last("limit 1");
        TbTeacher tbTeacher1 = tbTeacherMapper.selectOne(last1);
        if(tbTeacher==null){
            return ResultResponse.FAILED(-1,"参数异常");
        }


        Long teacherId = tbTeacher.getTeacherId();
        Long clazzId = tbTeacher.getClazzId();
        TbClazzTeacher tbClazzTeacher = new TbClazzTeacher();
        tbClazzTeacher.setClazzId(clazzId);
        tbClazzTeacher.setTeacherId(teacherId);
        int insert = clazzTeacherMapper.insert(tbClazzTeacher);

        return ResultResponse.SUCCESS("新增成功");

    }

    @Override
    public ResultResponse getClazzListVoByCollageId(TbCollage collage) {
        if(collage==null){
            return  ResultResponse.FAILED(-1,"参数异常");
        }
        ArrayList<ClazzListVo> clazzListVos = new ArrayList<>();
        Long collageId = collage.getCollageId();
        LambdaQueryWrapper<TbMajor> eq = new QueryWrapper<TbMajor>().lambda().eq(TbMajor::getCollageId, collageId);
        List<TbMajor> majors = majorMapper.selectList(eq);
        for (int i = 0; i < majors.size(); i++) {
            ClazzListVo clazzListVo = new ClazzListVo();
            clazzListVo.setId(majors.get(i).getMajorId());
            clazzListVo.setName(majors.get(i).getMajorName());

            LambdaQueryWrapper<TbStage> eq1 = new QueryWrapper<TbStage>().lambda().eq(TbStage::getMajorId, majors.get(i).getMajorId());
            List<TbStage> stages = stageMapper.selectList(eq1);

            List<ClazzListVo> clazzListVos1 =new ArrayList<>();

            for (int i1 = 0; i1 < stages.size(); i1++) {
                ClazzListVo clazzListVo1 = new ClazzListVo();
                clazzListVo1.setId(stages.get(i1).getStageId());
                clazzListVo1.setName(stages.get(i1).getStageName());
                List<ClazzListVo> clazzListVos2 = new ArrayList<>();
                LambdaQueryWrapper<TbClazz> eq2 = new QueryWrapper<TbClazz>().lambda().eq(TbClazz::getStageId, stages.get(i1).getStageId());
                List<TbClazz> tbClazzes = tbClazzMapper.selectList(eq2);
                for (int i2 = 0; i2 < tbClazzes.size(); i2++) {
                    ClazzListVo clazzListVo2 = new ClazzListVo();
                    clazzListVo2.setName(tbClazzes.get(i2).getClazzName());
                    clazzListVo2.setId(tbClazzes.get(i2).getClazzId());
                    clazzListVos2.add(clazzListVo2);
                }
                clazzListVo1.setClazzListVos(clazzListVos2);
                clazzListVos1.add(clazzListVo1);
            }
            clazzListVo.setClazzListVos(clazzListVos1);
            clazzListVos.add(clazzListVo);
        }

        return ResultResponse.SUCCESS(clazzListVos);

    }
}




